<template>
  <div>
    <n-card>
      <HeadSearch :info="search" @search="handleSearch" @reset="handleSearch" />
      <DataTable
        ref="tableRef"
        :columns="columns"
        :pagination="false"
        :data="tableData"
        striped
        :row-key="(row) => row.id"
      >
        <template #tableTitle>
          <n-button
            v-if="permissionList.includes('system:menu:create')"
            type="primary"
            @click="handleModal"
          ><n-icon size="18"><AddOutline /></n-icon> {{ getPermissionLabel('system:menu:create') }} </n-button>
        </template>
      </DataTable>
    </n-card>
  </div>
  <!-- 新增、编辑弹窗 -->
  <MenuModal v-if="modalShow" v-model:visible="modalShow" :source="menuSource" :data="rowData" :type="modalType" @reload="handleSearch" />
</template>

<script>
import search from './tools/search.js'
import table from './tools/table.js'
import HeadSearch from '@/components/Search/index.vue'
import DataTable from '@/components/DataTable/index.vue'
import MenuModal from './components/MenuModal.vue'
import { toRefs, reactive, onUnmounted } from 'vue'
import { usePermissionStore } from '@/store/modules/permission'

export default {
  name: 'MenuPage',
  components: { DataTable, MenuModal, HeadSearch },
  setup() {
    const permissionStore = usePermissionStore()
    const data = reactive({
      ...toRefs(table),
      search,
      permissionList: permissionStore.accessPermissionCodes
    })
    table.fetchList()

    const getPermissionLabel = (code) => {
      return permissionStore.accessPermissions.find((item) => {
        return item.code === code
      })?.name || ''
    }

    // 打开新增弹框
    function handleModal() {
      data.rowData = null
      data.modalType = 'create'
      data.modalShow = true
    }

    onUnmounted(() => {
      data.searchParams = { }
    })

    return {
      ...toRefs(data),
      handleModal,
      getPermissionLabel
    }
  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
